/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.math.BigDecimal;
import java.security.MessageDigest;
import java.text.NumberFormat;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;
import sun.misc.BASE64Encoder;

/**
 *
 * @author Fabio
 */
public class Util {
    
    public static DefaultFormatterFactory setMascaraData(){  
        MaskFormatter comFoco = null;  
        try{   
            comFoco = new MaskFormatter("##/##/####");  
        }   
        catch (Exception pe) { }  
        DefaultFormatterFactory factory = new DefaultFormatterFactory(comFoco, comFoco);  
        return factory;  
    }
    
    public static DefaultFormatterFactory setMascaraMoeda(){  
        MaskFormatter comFoco = null;  
        try{   
            comFoco = new MaskFormatter("##########,##");
        }   
        catch (Exception pe) { }  
        DefaultFormatterFactory factory = new DefaultFormatterFactory(comFoco, comFoco);  
        return factory;  
    }
    
    public static DefaultFormatterFactory setMascaraCEP(){  
        MaskFormatter comFoco = null;  
        try{   
            comFoco = new MaskFormatter("#####-###");  
        }   
        catch (Exception pe) { }  
        DefaultFormatterFactory factory = new DefaultFormatterFactory(comFoco, comFoco);  
        return factory;  
    }
    
    public static DefaultFormatterFactory setMascaraFone(){  
        MaskFormatter comFoco = null;  
        try{   
            comFoco = new MaskFormatter("(###)####-##-##");  
        }   
        catch (Exception pe) { }  
        DefaultFormatterFactory factory = new DefaultFormatterFactory(comFoco, comFoco);  
        return factory;  
    }
    
    public static String FormataMoedaString(String as){
        double valor =  Double.parseDouble(as.replaceAll(",", "."));
        NumberFormat nf = NumberFormat.getCurrencyInstance();
        return nf.format(valor);
    }
    
    public static BigDecimal StrintToBigDecimal(String as){
        BigDecimal result = null;
        as = as.replaceAll("\\.", "");
        as = as.replace("R$ ", "");
        result = new BigDecimal(as.replaceAll(",", "."));
        return result;
    }
    
    public static Double StrintToDouble(String as){
        Double result = null;
        as = as.replaceAll("\\.", "");
        as = as.replace("R$ ", "");
        result = new Double(as.replaceAll(",", "."));
        return result;
    }
    
    public static String encripta(String senha){
        try {
            //digest criado com o algoritimo MD5
            MessageDigest digest = MessageDigest.getInstance("MD5");
            
            //o método update adiciona bytes a mensagem a ser criptografada
            digest.update(senha.getBytes());
            
            // cria-se um encoder para impedir que a senha excriptada apareça 
            //com caracteres que costuman ausar problemas em xml a alguns bancos de dados
            BASE64Encoder encoder = new BASE64Encoder();
            
            //o método digest.digest() retorna a mensagem encripada
            //o metodo encoder.encode() subistitui os caracteres problemáticos
            return encoder.encode(digest.digest());
            
        } catch (Exception e) {
            e.printStackTrace();
            return senha;
        }
    }
}